package com.qf.j2112.controller;

import javax.servlet.http.HttpServletRequest;

import com.qf.j2112.po.SysCart;
import com.qf.j2112.po.SysGoods;
import com.qf.j2112.service.RemoteGoodsService;
import com.qf.j2112.service.SysCartService;
import com.qf.j2112.utils.JwtUtil;
import com.qf.j2112.vo.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 购物车管理Controller
 * @author j2112
 * @date 2022-04-12
 */

@Slf4j
@CrossOrigin
@RestController
public class SysCartController {

    @Autowired
    private SysCartService sysCartService;

    @Autowired
    private RemoteGoodsService remoteGoodsService;

    /*查询用户购物车列表*/
    @RequestMapping(value = "/cartlist",method = RequestMethod.GET)
    public AjaxResult list(HttpServletRequest request) {
        String token=request.getHeader("token");
        token=JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析为:"+userId);
        List<SysCart> list = sysCartService.selectSysCartList(userId);
        if(!ObjectUtils.isEmpty(list)){
            return AjaxResult.success("查询成功",list);
        }else {
            return AjaxResult.error("查询失败",list);
        }
    }

    /*查询用户购物车内商品数量*/
    @RequestMapping(value = "/cartnum",method = RequestMethod.GET)
    public AjaxResult num(HttpServletRequest request) {
        String token=request.getHeader("token");
        token=JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析为:"+userId);
        int size=sysCartService.selectSysCartNumByUserId(userId);
        return AjaxResult.success("查询成功",size);
    }

    /*添加商品到购物车*/
    @PostMapping
    @RequestMapping(value = "/cartadd",method = RequestMethod.GET)
    public AjaxResult add(Integer goodsId, HttpServletRequest request) {
        String token=request.getHeader("token");
        token=JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析"+token);
        SysCart sysCart=new SysCart();
        sysCart.setCartUserId(userId);
        sysCart.setCartGoodsId(goodsId);
        sysCart.setCartGoodsNum(1);
        SysGoods sysGoods=remoteGoodsService.getGoodsInfo(goodsId);
        sysCart.setCartGoodsName(sysGoods.getGoodsName());
        sysCart.setCartGoodsDesc(sysGoods.getGoodsDesc());
        sysCart.setCartGoodsPrice(sysGoods.getGoodsOriginprice()*sysGoods.getGoodsSale());
        sysCart.setCartGoodsPic(sysGoods.getGoodsPic());
        boolean flag=sysCartService.insertSysCart(sysCart);
        if(flag){
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error("添加失败");
        }
    }

    /*修改购物车*/
    @RequestMapping(value = "cartupdate",method = RequestMethod.PUT)
    public AjaxResult edit(@RequestBody SysCart sysCart) {
        log.info("接收到参数"+sysCart);
        return AjaxResult.success(sysCartService.updateSysCart(sysCart));
    }

    /*删除购物车某件商品*/
    @RequestMapping(value ="/delcart/{cartId}",method = RequestMethod.DELETE)
    public AjaxResult remove(@PathVariable Integer cartId) {
        return AjaxResult.success(sysCartService.deleteSysCartByCartId(cartId));
    }


}
